//
//  XHBTabBarController.m
//  iOS Object
//
//  Created by 保密 on 2019/6/27.
//  Copyright © 2019年 XiaoHaiBao. All rights reserved.
//

#import "XHBTabBarController.h"

#import "XHBNavigationController.h"
#import "XHBFirstVC.h"
#import "XHBSecondVC.h"
#import "XHBThirdVC.h"
#import "XHBFourthVC.h"
#import "XHBFifthVC.h"

@interface XHBTabBarController ()<XHBBaseTabBarControllerDelegate>

@end

@implementation XHBTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 默认为YES, 显示的是半透明的效果, 能够模糊看到被bar遮盖的东西, 如果设置为NO, 则没有模糊透明的效果
    self.tabbar.translucent = NO;
    
    // 中间凹凸样式
    self.tabbar.centerOffsetY = -15.0f;
    self.tabbar.position = XHBTabBarCenterButtonPositionCenter;
    self.tabbar.centerImage = [UIImage imageNamed:@"tab_bar_ optional_red"];
    
    self.xhb_delegate = self;
    
    [self addViewController];
}

// 添加控制器
- (void)addViewController {
    
    // 第一页
    [self addChildrenViewController:[[XHBFirstVC alloc] init] title:@"第一页" normalImage:@"首页" selectedImage:@"首页_selected"];
    // 第二页
    [self addChildrenViewController:[[XHBSecondVC alloc] init] title:@"第二页" normalImage:@"消息" selectedImage:@"消息_selected"];
    
    // 第三页 <中间自定义>
    [self addChildrenViewController:[[XHBThirdVC alloc] init] title:@"第三页" normalImage:@"" selectedImage:@""];
    
    // 第四页
    [self addChildrenViewController:[[XHBFourthVC alloc] init] title:@"第四页" normalImage:@"搜索" selectedImage:@"搜索_selected"];
    // 第五页
    [self addChildrenViewController:[[XHBFifthVC alloc] init] title:@"第五页" normalImage:@"我的" selectedImage:@"我的_selected"];
    
}

- (void)addChildrenViewController:(UIViewController *)childVC title:(NSString *)title normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage {
    
    childVC.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    childVC.tabBarItem.selectedImage =  [UIImage imageNamed:selectedImage];
    
    childVC.title = title;
    
    //    XHBNavigationController
    XHBNavigationController *baseNav = [[XHBNavigationController alloc] initWithRootViewController:childVC];
    
    [self addChildViewController:baseNav];
}

// 使用XHBTabBarController 自定义的 选中代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 2){
        [self rotationAnimation];
    }else {
        [self.tabbar.centerBtn.layer removeAllAnimations];
    }
}

//旋转动画
- (void)rotationAnimation{
    if ([@"key" isEqualToString:[self.tabbar.centerBtn.layer animationKeys].firstObject]){
        return;
    }
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    rotationAnimation.duration = 3.0;
    rotationAnimation.repeatCount = HUGE;
    [self.tabbar.centerBtn.layer addAnimation:rotationAnimation forKey:@"key"];
}

@end
